
#define abs(x) (x<0?-x:x)

void sobel_c (unsigned char *src, unsigned char *dst, int h, int w, int row_size) {
unsigned char pxls,pxlr;
int result = 0;
int filtroX,filtroY;
unsigned char *pxl,*pxlaux;
unsigned char *grabar, *grabaraux;

pxl = src + w + 1; // fila 2, columna 2
grabar = dst + w + 1; 
pxlaux = pxl;
grabaraux = grabar;
int i,j;

	for(i = 0; i < h - 2; i++){
		pxl = pxlaux;
                grabar = grabaraux;
	    for(j = 0; j < w - 2; j++){
		result = 0;
		filtroX = *(pxl - row_size + 1)
			  - *(pxl - row_size - 1)
			  + 2*(*(pxl + 1))
			  - 2*(*(pxl - 1))
			  + (*(pxl + row_size + 1))
			  - (*(pxl + row_size - 1));

		filtroY = (*(pxl + row_size + 1))
			  + (*(pxl + row_size - 1))
			  + 2*(*(pxl + row_size))
			  - *(pxl - row_size + 1)
			  - *(pxl - row_size - 1)
			  - 2*(*(pxl - row_size));
		

		filtroX = abs(filtroX);
		filtroY = abs(filtroY);

		result  = filtroX + filtroY;

		if(result > 0xFF){
 
			*grabar = (unsigned char) 0xFF;

		} else {

			*grabar = (unsigned char)result;
		}

		pxl++;
		grabar++;
	    }
 	    pxlaux = pxlaux + row_size;
            grabaraux = grabaraux + row_size;

	}
}

